<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">
    <title>欢迎页面-L-admin1.0</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi"/>
    <link rel="shortcut icon" href="/favicon.ico" type="image/x-icon"/>
    <link rel="stylesheet" href="../css/font.css">
    <link rel="stylesheet" href="../css/xadmin.css">
    <script src="../js/jquery.min.js"></script>
    <script type="text/javascript" src="../lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="../js/xadmin.js"></script>
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
    <script src="https://cdn.staticfile.org/html5shiv/r29/html5.min.js"></script>
    <script src="https://cdn.staticfile.org/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>

<body>
<div class="x-body">
    <form class="layui-form">
        <div class="layui-form-item">
            <label for="" class="layui-form-label">
                <span class="x-red">*</span>商品名称
            </label>
            <div class="layui-input-inline">
                <input type="text" id="gname" name="gname" required lay-verify="required"
                       autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">
                <span class="x-red">*</span>商品图片
            </label>
            <div class="layui-upload">
                <button type="button" class="layui-btn" id="uploadImg">上传图片</button>
                <div class="layui-upload-list" style="border: 0 solid transparent; margin-left: 110px">
                    <img class="layui-upload-img" id="gimg" style="border: 0 solid transparent;width: 80px;height: 80px">
                    <p style="display: inline" id="demoText"></p>
                </div>
                <input type="hidden" id="gimgSrc" name="gimgSrc"
                       autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label for="" class="layui-form-label">
                <span class="x-red">*</span>商品单价
            </label>
            <div class="layui-input-inline">
                <input type="text" id="gprice" name="gprice" required lay-verify="required"
                       autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label for="" class="layui-form-label">
                <span class="x-red">*</span>商品数量
            </label>
            <div class="layui-input-inline">
                <input type="text" id="gcount" name="gcount" required lay-verify="required"
                       autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label for="" class="layui-form-label">
                <span class="x-red">*</span>商品类型
            </label>
            <div class="layui-input-inline">
                <input type="text" id="type" name="type" required lay-verify="required"
                       autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item layui-form-text">
            <label for="" class="layui-form-label">
                商品描述
            </label>
            <div class="layui-input-block">
                <textarea placeholder="请输入内容" id="gdetails" name="gdetails" class="layui-textarea"></textarea>
            </div>
        </div>

        <div class="layui-form-item">
            <label for="" class="layui-form-label">
                <span class="x-red">*</span>商品颜色
            </label>
            <div class="layui-input-inline">
                <input type="text" id="gcolor" name="gcolor" required lay-verify="required"
                       autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label for="" class="layui-form-label">
            </label>
            <button class="layui-btn" lay-filter="add" lay-submit="">
                增加
            </button>
        </div>
    </form>
</div>
<script>
    layui.use(['form', 'layer', 'upload'], function () {
        $ = layui.jquery;
        var form = layui.form
            , layer = layui.layer
            , upload = layui.upload;

        //普通图片上传
        var uploadInst = upload.render({
            elem: '#uploadImg'
            ,url: '../goods/addGoods/uploadImg'
            ,method:'POST'
            ,field: 'file'
            ,before: function(obj){
                //预读本地文件示例，不支持ie8
                obj.preview(function(index, file, result){
                    /*$('#gimgSrc').val(file.name);
                    $('#gimg').attr('src', result); //图片链接（base64）*/
                });
            }
            ,done: function(res){
                console.log(res.data)

                let src = res.data.substr(res.data.indexOf(":")+1,res.data.length);
                console.log(src)
                //如果上传失败
                /*if(res.code > 0){
                    return console.info(res);
                }*/

                //上传成功
                $('#gimgSrc').val(src);
                $('#gimg').attr('src','../res/static/img/'+src);

            }
            ,error: function(){
                //演示失败状态，并实现重传
                var demoText = $('#demoText');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function(){
                    uploadInst.upload();
                });
            }
        });


        //自定义验证规则
        form.verify({
            nikename: function (value) {
                if (value.length < 5) {
                    return '昵称至少得5个字符啊';
                }
            }
            , pass: [/(.+){6,12}$/, '密码必须6到12位']
            , repass: function (value) {
                if ($('#L_pass').val() != $('#L_repass').val()) {
                    return '两次密码不一致';
                }
            }
        });

        //监听提交
        form.on('submit(add)', function (data) {
            console.log(data.field)
            let gid = data.field.gid;
            let gname = data.field.gname;
            let gimg = 'res/static/img/'+data.field.gimgSrc;
            let gprice = data.field.gprice;
            let num = data.field.gcount;
            let type = data.field.type;
            let gdetails = data.field.gdetails;
            let gcolour = data.field.gcolor;

            $.ajax({
                type: 'POST',
                url: '../goods/addGoods',
                data: 'gid=' + gid + '&gName=' + gname + '&gImg=' + gimg + '&gPrice=' + gprice +
                    '&num=' + num + '&type=' + type + '&gDetails=' + gdetails + '&gColour=' + gcolour,
                success: function (data) {
                    if (parseInt(data) === 1) {
                        //发异步，把数据提交给php
                        layer.alert("增加成功", {icon: 6}, function () {
                            // 获得frame索引
                            var index = parent.layer.getFrameIndex(window.name);
                            //关闭当前frame
                            parent.layer.close(index);
                            //重新加载父页面
                            parent.location.reload();
                        });
                    }
                }
            })
            return false;
        });


    });
</script>
</body>

</html>